package nc.ui.pub.filesystem;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.tree.TreeNode;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.ui.pub.ClientEnvironment;
import nc.ui.sm.login.ClientAssistant;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.NCFileNode;

public class FileManagePopupMenu extends JPopupMenu{
	private static final long serialVersionUID = 8403367398548783316L;
	private  class MenuItemAction extends AbstractAction{
		private static final long serialVersionUID = -5821952918136673320L;
		NCFileNode node = null;
		public MenuItemAction(NCFileNode node) {
			super();
			this.node = node;
			putValue(AbstractAction.NAME, node.getName());
			putValue(AbstractAction.SMALL_ICON, getIconByName(node.getName()));
		}
		public void actionPerformed(ActionEvent e) {
			String path = node.getFullPath();
			String dsName = ClientEnvironment.getInstance().getConfigAccount().getDataSourceName();
			try {
				FileManageServletClient.showFileInWeb(dsName, path);
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}
		Icon getIconByName(String name){
			Icon icon = null;
			int index = name.lastIndexOf(".");
			if (index != -1) {
				String ext = name.substring(index + 1).toLowerCase();
				icon = ClientAssistant.loadImageIcon("images/filesystem/" + ext + ".png");
			}
			if(icon == null){
				icon = ClientAssistant.loadImageIcon("images/filesystem/default.png");
			}
			return icon;
		}

	}
	private String rootDirStr = null;
	public FileManagePopupMenu(String rootDirStr) {
		super();
		this.rootDirStr = rootDirStr.replace('\\', '/');
		if(this.rootDirStr.endsWith("/")){
			this.rootDirStr = this.rootDirStr.substring(0, this.rootDirStr.length()-1);
		}
		initalized();
	}


	private void initalized(){
		NCFileNode rootNode = null;
		if (rootDirStr != null) {
			IFileSystemService service = NCLocator.getInstance().lookup(IFileSystemService.class);
			NCFileNode node;
			try {
				node = service.getNCFileNodeTreeAndCreateAsNeed(rootDirStr, ClientEnvironment.getInstance().getUser().getUserName());
				rootNode = FileManageUI.findNode(node, rootDirStr);
			} catch (BusinessException e) {
				e.printStackTrace();
				Logger.error(e.getMessage() , e);
			}
			
		} 
		if(rootNode != null){
			int childCount = rootNode.getChildCount();
			for (int i = 0; i < childCount; i++) {
				TreeNode cNode = rootNode.getChildAt(i);
				if(cNode instanceof NCFileNode){
					NCFileNode theNode = (NCFileNode)cNode;
					if(theNode.getAllowsChildren()){
						JMenu menu = new JMenu(theNode.getName());
						construnctMenu(menu, theNode);
						add(menu);
					}else{
						MenuItemAction action = new MenuItemAction(theNode);
						add(action);
					}
					
				}
			}
		}
	
	}
	private  void construnctMenu(JMenu parent, NCFileNode node){
		int childCount = node.getChildCount();
		for (int i = 0; i < childCount; i++) {
			TreeNode cNode = node.getChildAt(i);
			if(cNode instanceof NCFileNode){
				NCFileNode theNode = (NCFileNode)cNode;
				if(theNode.getAllowsChildren()){
					JMenu menu = new JMenu(theNode.getName());
					construnctMenu(menu, theNode);
					parent.add(menu);
				}else{
					MenuItemAction action = new MenuItemAction(theNode);
					parent.add(action);
				}
				
			}
		}
	
	}

}
